#ifndef H_RCGD_MAINFUNC_H
#define H_RCGD_MAINFUNC_H

#define IsRefLocked()	(((REFERENCE_MODE==EB_STATUS_RUN)&&(SYSTEM.EB.LOCKMODE==EB_LOCKED))||\
							((REFERENCE_MODE==E1_STATUS_RUN)&&(SYSTEM.E1.LOCKMODE==E1_LOCKED))||\
							((REFERENCE_MODE==E2_STATUS_RUN)&&(SYSTEM.E2.LOCKMODE==E1_LOCKED))||\
							((REFERENCE_MODE==LMU_STATUS_RUN)&&(lmu.LOCKMODE == LMU_LOCKED)))
#define IsRefUnlocked()	(!(((REFERENCE_MODE==EB_STATUS_RUN)&&(SYSTEM.EB.LOCKMODE==EB_LOCKED))||\
							((REFERENCE_MODE==E1_STATUS_RUN)&&(SYSTEM.E1.LOCKMODE==E1_LOCKED))||\
							((REFERENCE_MODE==E2_STATUS_RUN)&&(SYSTEM.E2.LOCKMODE==E1_LOCKED))||\
							((REFERENCE_MODE==LMU_STATUS_RUN)&&(lmu.LOCKMODE == LMU_LOCKED))))
/* Position Survey */
#define SURVEY_OFF      0
#define SURVEY_ON       1
#define SURVEY_PAUSE    2
#define SURVEY_END      3



#define	TEMPER_SENSOR		0
#define CURRENT_SENSOR		1


/*=================================================================*/
/*                       MAINFUNC.C                                */
/*=================================================================*/
/* Check Lock ; */
typedef struct CL_S{
	char     AntennaFaultTable[10];

    int     LockedCounter;
    int     UnlockedCounter;
    int     HoldoverCounter;
    int		HoldoverDelayCounter;
} CL_;

#define TEMPERATURE_AVERAGE_INTERVAL 10
#define CURRENT_AVERAGE_INTERVAL	30

typedef struct TEMPER_S{
    float   AverageTable[TEMPERATURE_AVERAGE_INTERVAL];
    float   CURRENT;
    float   AVERAGE;

    int     InsertCounter;

    unsigned short  Low,Mid,Mid2,High,Counter;
    char    Lowp;
    char    Turn;

    float	current;
    float	current_average;
    int     CurrentInsertCounter;

    float   CurrentAverageTable[CURRENT_AVERAGE_INTERVAL];

} TEMPER_;




typedef struct{

	int	parity_error_counter;
	int	mode_error_counter;
	int	parity_1_error_counter;
	int	mode_1_error_counter;
	int	unlock_counter;

} TR_;



extern TR_	tr1;


/*=================================================================*/
/*                        type definition                          */
/*=================================================================*/



extern void Init_MAINFUNC(void);
extern void CheckLock();

extern void gwait(int);
extern void AverageTemperature(float t);
extern void AverageCurrent(float t);




extern void ChangeBaudRate(char,char);
extern unsigned short CheckCRC16(unsigned char *,int);
extern unsigned short CheckCRC16_C(unsigned char *, int,  unsigned short);


extern void reset_system();

extern char	prev_current_sensor_check;
extern char current_sensor_check;

extern void EB_STATUS_OUT();

extern char	hex_char[16];

extern char	REFERENCE_MODE;
extern char	PREV_REFERENCE_MODE;



extern int doubleNaN(double x);
extern int floatNaN(float x);

extern int censor_check_counter,censor_check_counter2;


extern int gps_holdover_counter;
extern int lmu_holdover_counter;
extern int e1_holdover_counter;
extern int e2_holdover_counter;
extern char gps_detect_flag;
extern char lmu_detect_flag;
extern char e1_detect_flag;
extern char e2_detect_flag;

extern char gps_lock_flag;
extern char lmu_lock_flag;
extern char e1_lock_flag;
extern char e2_lock_flag;

#define Holdover_limit	600



extern void EB_STATUS_OUT_kdy();
extern unsigned short calc_crc16(unsigned char *buf, int len);
char is_holdover_condition();
void reset_eb_counter();


#endif
